

clear all
set more off
set trace off
set tracedepth 1 

global ipvOutcomes any_violence phy_violence psy_violence // cont_behavior arguments 
global dumOutcomes dAny   dPsy dPhy // dCont dArgs dPsyPhy

global incOutcomes  income_hh log_income_hh ihs_income_hh income_dummy income_woman_mid income_man_mid  days_out  

global mentalHealth anxiety sentimental loneliness rage raise_voice violence









/*---------------------------------------------------------------------------
----- Esttab globals
----------------------------------------------------------------------------*/

global joinMgroupSetting 	prefix(\multicolumn{@span}{c}{) suffix(}) ///
	span erepeat(\cmidrule(lr){@span})

	
	


global stars_settings 	 star(* 0.10 ** 0.05 *** 0.01)   se(a3) b(a3) ///

global regOutputFormattingHelp 		noomitted ///
		varwidth(35) ///
		compress   ///
		$stars_settings  ///
		label nobaselevels interaction("$\times$") 
		
		
		
global regOutputSettings $regOutputFormattingHelp numbers replace substitute(\_ _ "=1" "" ) 
global regOutputSettNoNumb $regOutputFormattingHelp nonumbers substitute(\_ _ "=1" "" ) 


global incomeLabs   		mtitles("Levels" "Log" "$>$0" "IHS" "Out" "$>$0" ) ///
		mgroups("Income" "Income" "Income" "Income" "Days" "Alcohol", pattern( 1 1 1 1 1 1  ) span) 

// you need the special cell command in the latex file \newcommand{\specialcell}[2][c]{\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
// which is copied from https://tex.stackexchange.com/questions/2441/how-to-add-a-forced-line-break-inside-a-table-cell/19678#19678
global tabSettings 	booktabs	prehead( \begin{tabular}{l*{@M}{c}} \toprule\toprule) postfoot(\bottomrule \bottomrule \end{tabular})   

global sector_clusters s( ymean N   ,label( "Outcome Mean (2019)"   "Observations"   ))

global district_clusters s( ymean N N_clust1  ,label( "Outcome Mean (2019)"   "Observations" "Districts"  ))
global district_clusters s( ymean N   ,label( "Outcome Mean (2019)"   "Observations"   ))



cap program drop addScalars
program define addScalars
	//syntax namelist(max=1)
	//qui: estadd ysumm 
	preserve
		qui: keep if year == 2019
		qui: estadd ysumm
	restore

	
	
	quietly estadd local fixed_dis "Yes", replace
end	

**************************************************************************
****************** OPENING DATA SET
**************************************************************************

//anxiety sentimental loneliness rage raise_voice violence

use "$data_folder/NBER_HPI_PANEL_with_bartick_and_weights.dta", clear

xtset id_cases year
sort id_cases year
// xtpoisson ipv1_reg dummy_time ln_bartik_national, a(ubigeo) exposure(month) cluster(ubigeo)

**************************************************************************
****************** TABLE 2 : 2020 VS 2019
**************************************************************************
foreach  out in $ipvOutcomes $dumOutcomes {

	eststo time`out' : quietly ppmlhdfe `out' post_aprilmay post_julaug, a(id_cases) exposure(month) irr cluster(sector22)
	addScalars

}


** IPV time trends
esttab     timepsy_violence timephy_violence  timeany_violence ///
		using "$tex_folder/ipv_amja.tex" ///
		, ///
		 $sector_clusters  ///
		$tabSettings  $regOutputSettings   keep(*post* )
	

**************************************************************************
****************** TABLE 3 : 2020 VS 2019
**************************************************************************


//income and daysout results
foreach out in $incOutcomes {
	eststo e`out': reghdfe `out' post_aprilmay post_julaug, a(id_cases) vce(cluster sector22)
	addScalars	
}



		
** Income  time trends
esttab eincome_hh eincome_woman_mid eincome_man_mid   edays_out    ///
		using "$tex_folder/income_amja.tex" ///
		, ///
		 $sector_clusters  ///
		$tabSettings  $regOutputSettings   keep(*post* )
	



**Coef plots
eststo Arguments : quietly ppmlhdfe ipv1_reg post_aprilmay post_julaug, a(id_cases) exposure(month) irr cluster(sector22)
eststo ControlB : quietly ppmlhdfe ipv2_reg post_aprilmay post_julaug, a(id_cases) exposure(month) irr cluster(sector22)
eststo Psyco : quietly ppmlhdfe ipv3_reg post_aprilmay post_julaug, a(id_cases) exposure(month) irr cluster(sector22)
eststo Physical : quietly ppmlhdfe ipv4_reg post_aprilmay post_julaug, a(id_cases) exposure(month) irr cluster(sector22)
eststo AnyIPV : quietly ppmlhdfe ipv5_reg post_aprilmay post_julaug, a(id_cases) exposure(month) irr cluster(sector22)


// coefplot Arguments ControlB Psyco Physical AnyIPV , ///
// 				keep(post_aprilmay post_julaug) yline(0) vertical ///
// 				graphregion(color(white)) ///
// 				/*key(ci 1)*/ scheme(s2mono) ///
// 				title("IPV variation", size(medium)) ///
// 				note("{it: Note1: Post AM is the coefficient during April-May(2020) relative to 2019}" ///
// 				 "{it: Note2: Post JA is the coefficient during July-August(2020) relative to 2019}", size(small))		

// graph export "$graph_folder/ipv_amja_coef.png", as(png) replace
				
// eststo clear


	
**************************************************************************
****************** TABLE A5 : Bartick  Shock descriptives
*************************************************************************
//for pct_chng_wgt_v2 descriptive
estpost tabstat barChnQ219 psPhy_violence, statistics(p1 p10 p25 p50 p75 p99 mean) listwise
unique ubigeo
estadd scalar Districts `r(sum)'
esttab .  ///
 	/// using "$tex_folder/bartick_change_dsn.tex" ///
	, ///
	 cells("barChnQ219 " ) ///
	 collabel(,none) mtitle("\$g^{\text{shift-share}}_i\$") nonumbers ///
	 stats(N Districts) ///
	 	substitute(\_ _ "=1" "" ) ///
	$tabSettings replace

 
 

	
	
**************************************************************************
****************** ESTIMATES FOR  BARTIK AND SECTOR EMPLOYMENT
**************************************************************************

 
**i) Table 1: Bartik/Employment Variation and IPV
foreach out in $ipvOutcomes $incOutcomes $dumOutcomes  {
	local regType 
	local exposureSetting 

	if strpos( "$ipvOutcomes","`out'") > 0 {
		local regType ppmlhdfe
		local exposureSetting exposure(month)
		
	}
	
	else if (strpos( "$incOutcomes","`out'") > 0) | (strpos( "$dumOutcomes","`out'") > 0) {
		di "in if"
		local regType reghdfe
		local exposureSetting 
	}	
	
	di "`regType'"


	*sector (linear) v2 ( using 2019 as base year)
	eststo occLin_v2`out' : quietly `regType' `out' (post_aprilmay post_julaug)##c.pct_chng_wgt_v2, a(id_cases ) cluster(sector22) `exposureSetting'
	addScalars
	quietly estadd local fixed_sec "Yes", replace
	test 1.post_aprilmay#c.pct_chng_wgt_v2   = 1.post_julaug#c.pct_chng_wgt_v2  
	quietly estadd scalar pval `r(p)', replace
	
	
	* with covid
	eststo occLin_v2C`out' : quietly `regType' `out' (post_aprilmay post_julaug)##(c.pct_chng_wgt_v2 c.june_death_rate ), a(id_cases ) cluster(sector22) `exposureSetting'
	addScalars
	quietly estadd local fixed_sec "Yes", replace
	
	* with bartick shock
	eststo occBar`out' : quietly `regType' `out' (post_aprilmay post_julaug)##(c.pct_chng_wgt_v2 c.barChnQ219 ), a(id_cases ) cluster(sector22) `exposureSetting'
	addScalars

		

	*shocks  
	foreach shock in  barChnQ219     {
		eststo `shock'`out' : quietly  `regType' `out'  (post_aprilmay post_julaug)##c.`shock', a(id_cases) cluster(ubigeo) `exposureSetting'
		addScalars
		
	}
	
// 	foreach covidStat in  deaths cases   {
//		
// 		eststo `covidStat'`out' : quietly  `regType' `out'  (post_aprilmay post_julaug) `covidStat', a(id_cases) cluster(ubigeo) `exposureSetting'
// 		addScalars
//
//		
// 	}
//	
//	
	
	
}


**************************************************************************
****************** ESTIMATES FOR MENTAL HEALTH OUTCOMES
**************************************************************************



//the mentalh health outcomes are expressed as changes so just do an OLS 
// with the change on the RHS. 
foreach var in anxiety sentimental loneliness rage raise_voice violence  any_mental {
	
	eststo occLinM`var' : quietly reghdfe `var' pct_chng_wgt_v2, noabsorb cluster(sector22) 
	estadd ysumm
	eststo barChnQ219M`var' : quietly reghdfe `var' barChnQ219, noabsorb cluster(ubigeo) 
	estadd ysumm

}


**************************************************************************
****************** ESTIMATES FOR SYNTHETIC WEIGHTED REGRESSIONS
**************************************************************************


//weighted regressions
foreach out in  $dumOutcomes {
	eststo occWei`out' : qui: reghdfe `out'   (post_aprilmay post_julaug)##c.pct_chng_wgt_v2 [aw=unnormalized_weights],  a(id_cases ) cluster(sector22) 
	addScalars
	quietly estadd local fixed_sec "Yes", replace
	
	
	eststo occWeiIP`out' : qui: reghdfe `out'   (post_aprilmay post_julaug)##c.pct_chng_wgt_v2 [aw=unnormalized_weights_ipv],  a(id_cases ) cluster(sector22) 
	addScalars
	quietly estadd local fixed_sec "Yes", replace
	
	
}

**************************************************************************
****************** TABLE 7
**************************************************************************

esttab  occLinM* ///
	using "$tex_folder/mental_health_table.tex" ///
	, ///
	s( ymean N  ,label( "Outcome Mean (2020)"   "Observations"   )) ///
	$tabSettings  $regOutputSettings  keep(pct_chng_wgt_v2)
	

	
	

	






**************************************************************************
****************** TABLE A4
**************************************************************************
esttab    occLin_v2psy_violence occLin_v2phy_violence  occLin_v2any_violence /// IPV
	using "$tex_folder/occupation_lin_IPV_2020Q2_2019.tex" ///
	, ///
	 $sector_clusters ///
	$tabSettings  $regOutputSettings   keep(*post* )
	
 

**************************************************************************
****************** TABLE 5
**************************************************************************
esttab occLin_v2income_hh occLin_v2income_woman_mid occLin_v2income_man_mid   occLin_v2days_out    ///
	  using "$tex_folder/occupation_lin_inc_2020Q2_2019.tex" ///
	, ///
	s( ymean pval N    ,label( "Outcome Mean (2019)" "P-Value $\delta_1 = \delta_2$"   "Observations"   )) ///
	$tabSettings    keep(*post* ) ///
	$regOutputFormattingHelp ///
	substitute( ///
		"April-May (2020)=1$\times$$ g^{\text{sector}}_i" "($\delta_1$) April-May (2020)$\times$$ g^{\text{sector}}_i" ///
		"July-August (2020)=1$\times$$ g^{\text{sector}}_i" "($\delta_2$) July-August (2020)$\times$$ g^{\text{sector}}_i" ///
		"=1" ""    ///
	)  replace


	

**************************************************************************
****************** TABLE 4
**************************************************************************

esttab    occLin_v2dPsy occLin_v2dPhy  occLin_v2dAny ///
	  using "$tex_folder/occupation_lin_IPV_LPM_2020Q2_2019.tex" ///
	, ///
	s( ymean pval N    ,label( "Outcome Mean (2019)" "P-Value $\delta_1 = \delta_2$"   "Observations"   )) ///
	$tabSettings    keep(*post* ) ///
	$regOutputFormattingHelp ///
	substitute( ///
		"April-May (2020)=1$\times$$ g^{\text{sector}}_i" "($\delta_1$) April-May (2020)$\times$$ g^{\text{sector}}_i" ///
		"July-August (2020)=1$\times$$ g^{\text{sector}}_i" "($\delta_2$) July-August (2020)$\times$$ g^{\text{sector}}_i" ///
		"=1" ""    ///
	) replace
	
**************************************************************************
****************** TABLE B3
**************************************************************************

//weighted with synth weights
esttab    occWeidPsy occWeidPhy  occWeidAny ///
	using "$tex_folder/occupation_lin_IPV_LPM_SynthWeights.tex" ///
	, ///
	$sector_clusters ///
	$tabSettings  $regOutputSettings  keep(*post* )

**************************************************************************
****************** TABLE B4
**************************************************************************

//weighted with synth weights focused on IPV weights
esttab    occWeiIPdPsy occWeiIPdPhy  occWeiIPdAny ///
	using "$tex_folder/occupation_lin_IPV_LPM_SynthWeights_ipv.tex" ///
	, ///
	$sector_clusters ///
	$tabSettings  $regOutputSettings  keep(*post* )


	
	


**************************************************************************
****************** TABLE 6
**************************************************************************

esttab   occLin_v2dPsy occLin_v2dPhy  occLin_v2dAny /// 
	 using "$tex_folder/sector_with_covid_stackedLPM.tex" ///
	, ///
	$regOutputSettings ///
	noobs /// 
	keep( *post*#*)  order(*pct_chng_wgt_v2* *june_death_rate*) ///
	booktabs	prehead(\begin{tabular}{l*{@M}{c}} \toprule\toprule) postfoot("")      prefoot("")     


esttab   occLin_v2CdPsy occLin_v2CdPhy  occLin_v2CdAny /// 
	using "$tex_folder/sector_with_covid_stackedLPM.tex" ///
	, ///
	$sector_clusters ///
	$regOutputSettNoNumb append ///
	nomtitles ///
	keep( *post*#*)  order(*pct_chng_wgt_v2* *june_death_rate*) ///
	booktabs	prehead("") posthead("\midrule \\") postfoot(\bottomrule \bottomrule \end{tabular})   prefoot("\\ \midrule")     


	


**************************************************************************
****************** TABLE A3
**************************************************************************

esttab   occLin_v2dPsy occLin_v2dPhy  occLin_v2dAny /// 
	 using "$tex_folder/sector_with_bartik_stackedLPM.tex" ///
	, ///
	$regOutputSettings ///
	noobs /// 
	keep( *post*#*)  order(*pct_chng_wgt_v2*  ) ///
	booktabs	prehead(\begin{tabular}{l*{@M}{c}} \toprule\toprule) postfoot("")      prefoot("")     


esttab   occBardPsy occBardPhy  occBardAny  ///
	using "$tex_folder/sector_with_bartik_stackedLPM.tex" ///
	, ///
	$sector_clusters ///
	$regOutputSettNoNumb append ///
	nomtitles ///
	keep( *post*#*)  order(*pct_chng_wgt_v2* *barChnQ219*) ///
	booktabs	prehead("") posthead("\midrule \\") postfoot(\bottomrule \bottomrule \end{tabular})   prefoot("\\ \midrule")     

	


**************************************************************************
****************** TABLE A6
**************************************************************************
	
esttab   barChnQ219dPsy barChnQ219dPhy  barChnQ219dAny ///
	using "$tex_folder/bartick_shock_lin_IPV_LPM.tex" ///
	, ///
	$district_clusters ///
	$tabSettings  $regOutputSettings 

**************************************************************************
****************** TABLE A2
**************************************************************************
eststo save_reg : reg  savings pct_chng_wgt_v2 if year == 2020 , r
estadd ysumm
esttab  save_reg ///
	 using "$tex_folder/save_reg.tex" ///
	, ///
	$tabSettings  $regOutputSettings  noconstant ///
	s(ymean N, label( "Outcome Mean (2020)") )
